package com.muxue.module.ai.service.chat.impl;


import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.muxue.common.core.page.PageResult;
import com.muxue.common.utils.object.BeanUtils;
import com.muxue.module.ai.controller.chat.vo.conversation.AiChatConversationCreateMyReqVO;
import com.muxue.module.ai.controller.chat.vo.conversation.AiChatConversationPageReqVO;
import com.muxue.module.ai.controller.chat.vo.conversation.AiChatConversationUpdateMyReqVO;
import com.muxue.module.ai.mapper.dataobject.chat.AiChatConversationDO;
import com.muxue.module.ai.mapper.dataobject.model.AiChatModelDO;
import com.muxue.module.ai.mapper.mysql.chat.AiChatConversationMapper;
import com.muxue.module.ai.service.chat.AiChatConversationService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.List;

import static com.muxue.common.exception.ServiceException.exception;
import static com.muxue.module.ai.model.enums.ErrorCodeConstants.CHAT_CONVERSATION_MODEL_ERROR;
import static com.muxue.module.ai.model.enums.ErrorCodeConstants.CHAT_CONVERSATION_NOT_EXISTS;

/**
 * AI 聊天对话 Service 实现类
 *
 * @author fansili
 */
@Service
@Validated
@Slf4j
public class AiChatConversationServiceImpl implements AiChatConversationService {

    @Resource
    private AiChatConversationMapper chatConversationMapper;


    @Override
    public Long createChatConversationMy(AiChatConversationCreateMyReqVO createReqVO, Long userId) {
        return null;
    }

    @Override
    public void updateChatConversationMy(AiChatConversationUpdateMyReqVO updateReqVO, Long userId) {
        // 1.1 校验对话是否存在
        AiChatConversationDO conversation = validateChatConversationExists(updateReqVO.getId());
        if (ObjUtil.notEqual(conversation.getUserId(), userId)) {
            throw exception(CHAT_CONVERSATION_NOT_EXISTS);
        }
        // 1.2 校验模型是否存在（修改模型的情况）
        AiChatModelDO model = null;
        if (updateReqVO.getModelId() != null) {
        }

        // 1.3 校验知识库是否存在
        if (updateReqVO.getKnowledgeId() != null) {
        }

        // 2. 更新对话信息
        AiChatConversationDO updateObj = BeanUtils.toBean(updateReqVO, AiChatConversationDO.class);
        if (Boolean.TRUE.equals(updateReqVO.getPinned())) {
            updateObj.setPinnedTime(LocalDateTime.now());
        }
        if (model != null) {
            updateObj.setModel(model.getModel());
        }
        chatConversationMapper.updateById(updateObj);
    }

    @Override
    public List<AiChatConversationDO> getChatConversationListByUserId(Long userId) {
        return chatConversationMapper.selectListByUserId(userId);
    }

    @Override
    public AiChatConversationDO getChatConversation(Long id) {
        return chatConversationMapper.selectById(id);
    }

    @Override
    public void deleteChatConversationMy(Long id, Long userId) {
        // 1. 校验对话是否存在
        AiChatConversationDO conversation = validateChatConversationExists(id);
        if (conversation == null || ObjUtil.notEqual(conversation.getUserId(), userId)) {
            throw exception(CHAT_CONVERSATION_NOT_EXISTS);
        }
        // 2. 执行删除
        chatConversationMapper.deleteById(id);
    }

    @Override
    public void deleteChatConversationByAdmin(Long id) {
        // 1. 校验对话是否存在
        AiChatConversationDO conversation = validateChatConversationExists(id);
        if (conversation == null) {
            throw exception(CHAT_CONVERSATION_NOT_EXISTS);
        }
        // 2. 执行删除
        chatConversationMapper.deleteById(id);
    }

    @Override
    public AiChatConversationDO validateChatConversationExists(Long id) {
        return null;
    }

    private void validateChatModel(AiChatModelDO model) {
        if (ObjectUtil.isAllNotEmpty(model.getTemperature(), model.getMaxTokens(), model.getMaxContexts())) {
            return;
        }
        throw exception(CHAT_CONVERSATION_MODEL_ERROR);
    }



    @Override
    public void deleteChatConversationMyByUnpinned(Long userId) {
        throw exception(CHAT_CONVERSATION_MODEL_ERROR);
    }

    @Override
    public PageResult<AiChatConversationDO> getChatConversationPage(AiChatConversationPageReqVO pageReqVO) {
        return chatConversationMapper.selectChatConversationPage(pageReqVO);
    }

}
